package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.SpellUtils;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.PracticeInfoReport;
import com.ruoyi.system.domain.TrainingCadet;
import com.ruoyi.system.domain.TrainingCadetExpand;
import com.ruoyi.system.domain.TrainingInfoReport;
import com.ruoyi.system.mapper.TrainingCadetMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITrainingCadetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 培训学员信息Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class TrainingCadetServiceImpl implements ITrainingCadetService {
    @Autowired
    private TrainingCadetMapper trainingCadetMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Autowired
    ISysUserService userService;

    /**
     * 查询培训学员信息
     *
     * @param cadetId 培训学员信息主键
     * @return 培训学员信息
     */
    @Override
    public TrainingCadet selectTrainingCadetByCadetId(String cadetId) {
        return trainingCadetMapper.selectTrainingCadetByCadetId(cadetId);
    }

    /**
     * 查询培训对象当前扩展信息
     *
     * @param cadetId 培训学员信息主键
     * @return 培训对象扩展信息
     */
    @Override
    public TrainingCadetExpand selectNowExpandInfo(String cadetId) {
        return trainingCadetMapper.selectNowExpandInfo(cadetId);
    }

    /**
     * 查询培训学员信息列表
     *
     * @param trainingCadet 培训学员信息
     * @return 培训学员信息
     */
    @Override
    public List<TrainingCadet> selectTrainingCadetList(TrainingCadet trainingCadet) {
        return trainingCadetMapper.selectTrainingCadetList(trainingCadet);
    }

    /**
     * 新增培训学员信息
     *
     * @param trainingCadet 培训学员信息
     * @return 结果
     */
    @Override
    public int insertTrainingCadet(TrainingCadet trainingCadet) {
        trainingCadet.setCreateTime(DateUtils.getNowDate());
        if (trainingCadet.getMentorId() != null && trainingCadet.getMentorId().isEmpty()) {
            trainingCadet.setMentorId(null);
        }
        return trainingCadetMapper.insertTrainingCadet(trainingCadet);
    }

    /**
     * 修改培训学员信息
     *
     * @param trainingCadet 培训学员信息
     * @return 结果
     */
    @Override
    public int updateTrainingCadet(TrainingCadet trainingCadet) {
        if (trainingCadet.getMentorId() != null && trainingCadet.getMentorId().isEmpty()) {
            trainingCadet.setMentorId(null);
        }
        return trainingCadetMapper.updateTrainingCadet(trainingCadet);
    }

    /**
     * 批量删除培训学员信息
     *
     * @param cadetIds 需要删除的培训学员信息主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetByCadetIds(String[] cadetIds) {
        return trainingCadetMapper.deleteTrainingCadetByCadetIds(cadetIds);
    }

    /**
     * 删除培训学员信息信息
     *
     * @param cadetId 培训学员信息主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetByCadetId(String cadetId) {
        return trainingCadetMapper.deleteTrainingCadetByCadetId(cadetId);
    }

    /**
     * 实体数据补全
     *
     * @param trainingCadet 实体
     * @param userId        用户id
     * @param crateType     创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(TrainingCadet trainingCadet, Long userId, Integer crateType) {
        //主键
        if (trainingCadet.getCadetId() == null || "".equals(trainingCadet.getCadetId())) {
            trainingCadet.setCadetId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //编码
        if (trainingCadet.getCadetCode() == null || "".equals(trainingCadet.getCadetCode())) {
            trainingCadet.setCadetCode(SpellUtils.convertSpell(trainingCadet.getCadetName(), false));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (trainingCadet.getCreateBy() == null) {
                trainingCadet.setCreateBy(userId.toString());
            }
            if (trainingCadet.getCreateTime() == null) {
                trainingCadet.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (trainingCadet.getModifyBy() == null) {
            trainingCadet.setModifyBy(userId);
        }
        //修改时间
        if (trainingCadet.getModifyTime() == null) {
            trainingCadet.setModifyTime(DateUtils.getNowDate());
        }
    }

    /**
     * 实践内容统计
     *
     * @param baseId 基地Id
     * @return 实践内容统计
     * @tartTime 开始时间
     * @endTime 结束时间
     */
    @Override
    public List<PracticeInfoReport> selectPracticeInfoReport(Long baseId, Date startTime, Date endTime) {
        endTime = DateUtils.addDays(endTime, 1);
        return trainingCadetMapper.selectPracticeInfoReport(baseId, startTime, endTime);
    }

    /**
     * 培训内容统计
     *
     * @param baseId    基地Id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 培训内容统计
     */
    @Override
    public List<TrainingInfoReport> selectTrainingInfoReport(Long baseId, Date startTime, Date endTime) {
        endTime = DateUtils.addDays(endTime, 1);
        return trainingCadetMapper.selectTrainingInfoReport(baseId, startTime, endTime);
    }
}
